<?php


namespace App\Tools;


class LiveTools
{
    /**
     * 获取推流地址
     * @param $streamName 填写自定义的流名称 StreamName，例如：liveteststream。可以理解为唯一标识
     *
     * @return string
     */
    public static function getPushUrl($streamName) {
        $key = config('live.key'); //key为配置推流域名主key，可以去腾讯控制台->域名管理->管理->推流配置 里面可以看到主key
        $domain = config('live.push_url');//您的推流域名
        $time = date('Y-m-d H:i:s', strtotime('+365day'));//过期时间，自行设置 例：date('Y-m-d H:i:s', strtotime('+365day'))
        $txTime = strtoupper(base_convert(strtotime($time), 10, 16));
        //txSecret = MD5( KEY + streamName + txTime )
        $txSecret = md5($key . $streamName . $txTime);
        $ext_str = "?" . http_build_query(array(
                "txSecret" => $txSecret,
                "txTime" => $txTime
            ));

        return "rtmp://" . $domain . "/live/" . $streamName . (isset($ext_str) ? $ext_str : "");
    }



    /**
     * 获取 拉流 地址
     * 如果不传key和过期时间，将返回不含防盗链的url
     *
     * @param $streamName : 您用来区别不同推流地址的唯一流名称
     *
     * @return String url
     */
    public static function getLivePullUrl($streamName) {
        $domain = config('live.pull_url');//同上
        $key = config('live.key');//同上
        $time = date('Y-m-d H:i:s', strtotime('+365day'));//同上
        $txTime = strtoupper(base_convert(strtotime($time), 10, 16));
        //txSecret = MD5( KEY + streamName + txTime )
        $txSecret = md5($key . $streamName . $txTime);
        $ext_str = "?" . http_build_query(array(
                "txSecret" => $txSecret,
                "txTime" => $txTime
            ));
        return "http://" . $domain . "/live/" . $streamName . '.m3u8' . (isset($ext_str) ? $ext_str : "");
    }


}
